<template>
  <view class="scan">
    <view class="codeScan">
      <!-- #ifdef MP-WEIXIN -->
      <wx-scan ref="scanOrderRef" @getCode="getCode"/>
      <!-- #endif -->
      <!-- #ifdef APP-PLUS -->
      <app-scan ref="appScanRef" @getCode="getCode"></app-scan>
      <!-- #endif -->
    </view>
  </view>
</template>

<script setup>
import {ref} from 'vue'
import {onShow, onLoad} from '@dcloudio/uni-app';
import wxScan from './WxScan.vue'
import appScan from './AppScan.vue'
import {useCameraAuth} from './useCameraAuth'
import service from "@/common/service";

// 引入权限相关的hook
const {isIos, getCameraAuth, refusedCameraAuth} = useCameraAuth()

const emit = defineEmits(['getCodeResult'])

const code = ref('')
const scanOrderRef = ref(null)
const appScanRef = ref(null)

// 扫码code
const getCode = (barNumber) => {
  console.log('扫码结果', barNumber)
  code.value = barNumber.trim()
  emit('getCodeResult', code.value);
}

// 第一次缓存是否进行过相机请求状态
onShow(async () => {
  /* #ifdef APP-PLUS */
  if (!uni.getStorageSync('iosCameraAuth') && isIos.value) {
    service.setStorageSync("iosCameraAuth", true);
  } else {
    const bol = await getCameraAuth()
    if (!bol) {
      refusedCameraAuth()
      return
    }
  }
  /* #endif */
})
</script>

<style lang='scss' scoped>
.codeScan {
  height: 580rpx; // app-ios wx
}

</style>